package com.lky.swagger2pro.controller;

import com.lky.swagger2pro.model.Book;
import com.lky.swagger2pro.service.IBookService;
import com.lky.swagger2pro.util.JsonResponseBody;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@Api(value = "书本管理",tags = {"书本管理"}) //tags可以代替value属性
@RequestMapping("/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    /**
     * 查询书本信息
     * @ApiOperation参数：
     * 1) value:url的路径值
     * 2) tags:如果设置这个值、value的值会被覆盖
     * 3) notes:注释说明
     * 4) produces:返回的格式类型，例如："application/json，application/xml"
     * 5) consumes:接收请求参数的类型，例如："application/json"
     * 6) hidden:是否在文档中显示
     * @return
     */
    @ApiOperation(value = "查询所有书本信息", notes = "查询返回所有书本信息集合",
            produces = "application/json")
    @GetMapping(value="/queryAll")
    public JsonResponseBody<List<Book>> queryAll(){
        try {
            return bookService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponseBody<>(500,e.getMessage());
        }
    }

    /**
     * 新增书本信息
     * @ApiImplicitParam参数：
     * 1) value:参数代表的含义
     * 2) name:参数名称
     * 3) required:是否必须
     * 4) dataType:参数类型
     * 5) defaultValue:默认值
     * @param bookname
     * @param price
     * @param booktype
     * @return
     */
    @ApiOperation(value = "新增书本信息", notes = "新增书本信息"
            ,produces = "application/json",consumes = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name="bookname",value="书本名称",required = true,dataType = "String"),
            @ApiImplicitParam(name="price",value="书本价格",required = true,dataType = "Double"),
            @ApiImplicitParam(name="booktype",value="书本类型",required = true,dataType = "String")
    })
    @PostMapping("/addBook")
    public JsonResponseBody<?> addBook(@RequestParam String bookname,
                                      @RequestParam Double price,
                                      @RequestParam String booktype){
        return bookService.insert(Book.builder()
                .bookid(UUID.randomUUID().toString().replace("-",""))
                .bookname(bookname)
                .booktype(booktype)
                .price(price)
                .build());
    }


    /**
     * 修改书本信息
     * 注意：在这里可以不使用@ApiImplicitParam标注Swagger中的参数信息，
     *       因为在这里的输入参数是实体对象，而在实体对象中已经使用
     *       @ApiModel和@ApiModelProperty 定义了对象及对象属性信息
     * @param book
     * @return
     */
    @ApiOperation(value="修改书本信息",notes = "修改书本信息")
    @PostMapping("/editBook")
    public JsonResponseBody<?> editBook(@RequestBody Book book){
        return bookService.updateByPrimaryKey(book);
    }

    /**
     * 查询单个书本信息
     * @param bookid
     * @return
     */
    @ApiOperation(value="查询单个书本信息",notes = "查询单个书本信息")
    @ApiImplicitParam(value="书本ID",name="bookid",required = true,dataType = "String",
            defaultValue = "8f46b5018a6811e9a9c528d24413c293" )
    @GetMapping("/querySingleBook")
    public JsonResponseBody<Book> querySingleBook(String bookid){
        return bookService.selectByPrimaryKey(bookid);
    }

    @ApiOperation(value="查询返回Map集合",notes="查询返回Map集合")
    @GetMapping("/queryMap")
    public JsonResponseBody<Map<String,Object>> queryMap(){
        return bookService.queryMap();
    }
}
